<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="128px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主订单号" prop="mainOrderId">
        <el-input
          v-model="queryParams.mainOrderId"
          placeholder="请输入主订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的港" prop="unloadingPort">
        <el-input
          v-model="queryParams.unloadingPort"
          placeholder="请输入目的港"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['business:cargo-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <el-tabs type="border-card" @tab-click="tabClick" v-model="activeTab">
    <el-tab-pane name="1" :label="'待入仓('+sumaryArr[0]+')'"/>
    <el-tab-pane name="2" :label="'待包装('+sumaryArr[1]+')'"/>
    <el-tab-pane name="3" :label="'待出仓('+sumaryArr[2]+')'"/>
    <el-tab-pane name="4" :label="'待安检('+sumaryArr[3]+')'"/>
    <el-tab-pane name="5" :label="'待报关('+sumaryArr[4]+')'"/>
    <el-tab-pane name="6" :label="'待交单('+sumaryArr[5]+')'"/>
    <el-tab-pane name="7" :label="'运输中('+sumaryArr[6]+')'"/>
    <el-tab-pane name="8" :label="'已完成('+sumaryArr[7]+')'"/>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="客户公司" align="center" prop="companyName" />
      <el-table-column label="运单号" align="center" prop="mainOrderId" width="150" />
      <el-table-column label="订单号" align="center" prop="orderId" width="100" />
      <el-table-column label="目的港" align="center" prop="destinationCityName" />
      <el-table-column label="件数" align="center" prop="warehouseNumber" width="60"/>
      <el-table-column label="计划" align="center" prop="planStatus" :formatter="planStatusFormat" width="150"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="viewForm(scope.row)"
            v-hasPermi="['business:cargo-info:query']"
          >
            {{btnTitleInfo(scope.row)}}
          </el-button>
          <el-button
            link
            type="primary"
            v-show="scope.row.status != 8"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:cargo-info:update']"
          >
            补充信息
          </el-button>
          <el-button
            link
            type="danger"
            v-show="scope.row.status < 5"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:cargo-info:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </el-tabs>
    

  <!-- 表单弹窗：添加/修改 -->
  <CargoInfoForm ref="formRef" @success="callbackFunc" />
  <CargoInfoView ref="CargoInfoViewRef"/>
  <PackCheckForm ref="PackCheckFormRef" @success="callbackFunc" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { CargoInfoApi, CargoInfoVO } from '@/api/business/cargoinfo'
import CargoInfoForm from './CargoInfoForm.vue'
import CargoInfoView from './CargoInfoView.vue'
import { dateFormatter } from '@/utils/formatTime'
import {useUserStore} from '@/store/modules/user'
import PackCheckForm from './packCheckForm.vue'

/** 货物资料 列表 */
defineOptions({ name: 'CargoInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const userStore = useUserStore();

const loading = ref(true) // 列表的加载中
const list = ref<CargoInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  mainOrderId: undefined,
  unloadingPort: undefined,
  status: 1,
  serviceId:userStore.user.id
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const btnTitleInfo = (row)=>{
  if(activeTab.value != '3'){
    return '订单信息'
  }else{
    if(row.status == 3 && row.packStatus == 0){
      return "审核"
    }else{
      return '订单信息'
    }
  }
}

const planStatusFormat = (row, index, cell)=>{
  if(cell == 0){
    return "";
  }else if(cell == 1){
    return "待下发计划"
  }else if(cell == 2){
    return "计划中"
  }else if(cell == 3){
    return "计划完成"
  }else{
    return "";
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  
  try {
    const data = await CargoInfoApi.getCargoInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const callbackFunc = async()=>{
  sumaryInfo();
  getList();
}

const activeTab = ref("1");
const tabClick = (a)=>{
  if(queryParams.status == Number(a.paneName)){
    return;
  }else{
    queryParams.status = Number(a.paneName)
    handleQuery();
  }
}

const sumaryArr = ref([0, 0 , 0, 0, 0, 0, 0, 0])
const sumaryInfo = async () => {
    sumaryArr.value = [0, 0 , 0, 0, 0, 0, 0, 0];
    const data = await CargoInfoApi.sumaryCargoInfo({serviceId:userStore.user.id})
    for(let i = 0; i < data.length; i++){
      let d = data[i];
      sumaryArr.value[d.status-1] = d.num;
    }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const CargoInfoViewRef = ref();
const PackCheckFormRef = ref();
const viewForm = (row)=>{
  if(row.status == 3 && row.packStatus == 0){
    PackCheckFormRef.value.open(row.id);
  }else{
    CargoInfoViewRef.value.open(row.id, 'kefu');
  }
  
  
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CargoInfoApi.deleteCargoInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CargoInfoApi.exportCargoInfo(queryParams)
    download.excel(data, '货物资料.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  sumaryInfo();
  getList()
})
</script>
